package homework0713;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class FtpVisitor implements Runnable {
	private Socket sk;

	public FtpVisitor(Socket sk) {
		super();
		this.sk = sk;
	}

	private void visitToFtp() throws Exception {
		File file = new File("D:/");
		OutputStream outputStream = sk.getOutputStream();
		InputStream in = sk.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String input = "Hey";
		while (true) {
			outputStream.write("Now you are in my disk D:\\ \r\n".getBytes());
			outputStream.write("What do you want to do now? \r\n".getBytes());
			input = reader.readLine();
			if (input.equals("ls")) {
				String[] fs = file.list();
				for (String string : fs) {
					File file2 = new File("D:/"+string);
					if(file2.isDirectory())
					{
						string = "<dir>"+string;
					}
					outputStream.write((string+"\r\n").getBytes());			
				}
				outputStream.write(("finish!\n").getBytes());
				continue;
			}
			
			if (input.trim().startsWith("rm ")){
				String string = input.substring(2);
				string=string.trim();
				try {
					File file2 = new File("D:/"+string);
					if(file2.isDirectory())
					{
						outputStream.write((string+" is Directory. You can't delete it!\r\n").getBytes());
						outputStream.write(("finish!\n").getBytes());
					}
					else {
						file2.delete();
						outputStream.write((string+"has already delete.\r\n").getBytes());
						outputStream.write(("finish!\n").getBytes());
					}
					continue;
				} catch (Exception e) {
					outputStream.write((string+"can't found!\r\n").getBytes());
					outputStream.write(("finish!\n").getBytes());
					continue;
				}
			}
			
			if (input.trim().startsWith("download ")) {
				String string = input.substring(8);
				string=string.trim();
				try {
					File file2 = new File("D:/"+string);
					if(file2.isDirectory())
					{
						outputStream.write((string+" is Directory. You can't transport it!\r\n").getBytes());
						outputStream.write(("finish!\n").getBytes());
					}
					else {
						FileInputStream fis = new FileInputStream(file2);
						byte[] sendBytes = new byte[1024];  
						long l = file2.length();
						int length = 0;
						double sumL = 0;
						boolean bool = false;
			            while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {  
			                sumL += length;    
			                System.out.println("已传输："+((sumL/l)*100)+"%");  
			                outputStream.write(sendBytes, 0, length);  
			                outputStream.flush();  
			            }   
			            fis.close();
			            outputStream.write("\n".getBytes());
			            if(sumL==l){  
			                bool = true;  
			            }  
			            if(bool)
						outputStream.write((string+"has already transport.\r\n").getBytes());
			            outputStream.write(("finish!\n").getBytes());
			            continue;
					}
				} catch (Exception e) {
					outputStream.write((string+" can't found!\r\n").getBytes());
					outputStream.write(("finish!\n").getBytes());
					outputStream.write(("finish!\n").getBytes());
					continue;
				}
			}
			
			
			if (input.equals("bye")) {
				outputStream.write("GoodBye~".getBytes());
				outputStream.write(("finish!\n").getBytes());
				break;
			}
			else
			{
				outputStream.write("Not clear~\n".getBytes());
				outputStream.write(("finish!\n").getBytes());
			}

		}
		outputStream.close();
		sk.close();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			visitToFtp();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
